#! /usr/bin/env bash

#  __________________________________________________________________________
# /\                                                                         \
# \_|        ___        __                            _   _                  |
#   |       |_ _|_ __  / _| ___  _ __ _ __ ___   __ _| |_(_) ___  _ __       |
#   |        | || '_ \| |_ / _ \| '__| '_ ` _ \ / _` | __| |/ _ \| '_ \      |
#   |        | || | | |  _| (_) | |  | | | | | | (_| | |_| | (_) | | | |     |
#   |       |___|_| |_|_|  \___/|_|  |_| |_| |_|\__,_|\__|_|\___/|_| |_|     |
#   |                                                                        |
#   | cmd.sh (Command): Command Wrapper, wrap bash functions into a command  |
#   |           Copyright (C) 2012 - 2014 Juan Manuel Borges Caño            |
#   |                Motivated by smart, elegant, efficient,                 |
#   |              fast and durable quality volatile commands.               |
#   |  This command is a fun and nice recursion coding exercise of cmd.sh.   |
#   |                     _     _                                            |
#   |                    | |   (_) ___ ___ _ __  ___  ___                    |
#   |                    | |   | |/ __/ _ \ '_ \/ __|/ _ \                   |
#   |                    | |___| | (_|  __/ | | \__ \  __/                   |
#   |                    |_____|_|\___\___|_| |_|___/\___|                   |
#   |                                                                        |
#   |   This program is free software: you can redistribute it and/or modify |
#   |   it under the terms of the GNU General Public License as published by |
#   |    the Free Software Foundation, either version 3 of the License, or   |
#   |                   (at your option) any later version.                  |
#   |                                                                        |
#   |     This program is distributed in the hope that it will be useful,    |
#   |      but WITHOUT ANY WARRANTY; without even the implied warranty of    |
#   |      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     |
#   |               GNU General Public License for more details.             |
#   |                                                                        |
#   |    You should have received a copy of the GNU General Public License   |
#   |  along with this program.  If not, see <http://www.gnu.org/licenses/>. |
#   |   _____________________________________________________________________|_
#    \_/_______________________________________________________________________/

# The option helpers
function cmd_option_package { cmd_var_package="$1"; }
function cmd_option_cmd { cmd_var_cmd="$1"; }
function cmd_option_name { cmd_var_name="$1"; }
function cmd_option_version { cmd_var_version="$1"; }
function cmd_option_description { cmd_var_description="$1"; }
function cmd_option_explanation { cmd_var_explanation="$1"; }
function cmd_option_license { cmd_var_license="$1"; }
function cmd_option_homepage { cmd_var_homepage="$1"; }
function cmd_option_author { cmd_var_author="$1"; }
function cmd_option_blog { cmd_var_blog="$1"; }
function cmd_option_email { cmd_var_email="$1"; }
function cmd_option_usage { cmd_var_usage="$1"; }
function cmd_option_option { cmd_var_options+=("$1"); }
function cmd_option_example { cmd_var_examples+=("$1"); }
function cmd_option_extrahelp { cmd_var_extrahelp="$1"; }
function cmd_option_extranotes { cmd_var_extranotes="$1"; }
function cmd_option_day { cmd_var_day="$1"; }
function cmd_option_month { cmd_var_month="$1"; }
function cmd_option_year { cmd_var_year="$1"; }
function cmd_option_timestamp { cmd_var_timestamp="$1"; }
function cmd_option_init { cmd_var_init="$1"; }
function cmd_option_main { cmd_var_main="$1"; }

# ... and cmd, the program itself

# The cmd init function
function cmd_cmd_init
{
	unset cmd_var_package cmd_var_cmd cmd_var_name cmv_var_version cmd_var_description cmd_var_explanation cmd_var_license cmd_var_homepage cmd_var_author cmd_var_blog cmd_var_email cmd_var_usage cmd_var_options ccmd_var_examples md_var_extrahelp cmd_var_extranotes cmd_var_day cmd_var_month cmd_var_year cmd_var_timestamp cmd_var_init cmd_var_main
}

# The cmd main function (which recurses cmd.sh)
function cmd_cmd_main
{
	# The recursed cmd fields
	cmd_package="$cmd_var_package"
	cmd="$cmd_var_cmd"
	cmd_name="$cmd_var_name"
	cmd_version="$cmd_var_version"
	cmd_description="$cmd_var_description"
	cmd_explanation="$cmd_var_explanation"
	cmd_license="$cmd_var_license"
	if [[ "${cmd_var_homepage+_}" ]]; then cmd_homepage="$cmd_var_homepage"; else unset cmd_homepage; fi
	cmd_author="$cmd_var_author"
	if [[ "${cmd_var_blog+_}" ]]; then cmd_blog="$cmd_var_blog"; else unset cmd_blog; fi
	if [[ "${cmd_var_email+_}" ]]; then cmd_email="$cmd_var_email"; else unset cmd_email; fi
	cmd_usage="$cmd_var_usage"
	cmd_options=("${cmd_var_options[@]}")
	cmd_examples=("${cmd_var_examples[@]}")
	if [[ "${cmd_var_extrahelp+_}" ]]; then cmd_extrahelp="$cmd_var_extrahelp"; else unset cmd_extrahelp; fi
	if [[ "${cmd_var_extranotes+_}" ]]; then cmd_extranotes="$cmd_var_extranotes"; else unset cmd_extranotes; fi
	cmd_day="$cmd_var_day"
	cmd_month="$cmd_var_month"
	cmd_year="$cmd_var_year"
	cmd_timestamp="$cmd_var_timestamp"
	cmd_main="$cmd_var_main"
	
	# The recursed cmd environment
	source "$cmd_datadir/cmd.sh" "$@"
}

# The cmd fields
cmd_package="[@]pkg[@]"
cmd="cmd"
cmd_name="command"
cmd_version="[@]pkgversion[@]"
cmd_description="Command Wrapper"
cmd_explanation="command is an smart, elegant, efficient, fast and durable quality volatile commands wrapper. cmd wraps commands with a sophisticated programmatic technology."
cmd_license="[@]pkglicense[@]"
cmd_homepage="[@]pkghomepage[@]"
cmd_author="[@]pkgauthor[@]"
cmd_blog="[@]pkgblog[@]"
cmd_email="[@]pkgemail[@]"
cmd_usage="$cmd [OPTIONS] -- [CMDARGS]"
cmd_examples=("$cmd -- --help" "/testcmd and testcmdsh/See testcmdsh and testcmd full example commands for usage.///")
cmd_options=(
	"/k:/cmdpackage:/set the package field/cmd_option_package/PACKAGE/"
	"/c:/cmd:/set the command field/cmd_option_cmd/COMMAND/"
	"/n:/cmdname:/set the name field/cmd_option_name/NAME/"
	"/r:/cmdversion:/set the version field/cmd_option_version/VERSION/"
	"/d:/cmddescription:/set the description field/cmd_option_description/DESCRIPTION/"
	"/x:/cmdexplanation:/set the explanation field/cmd_option_explanation/EXPLANATION/"
	"/l:/cmdlicense:/set the license field/cmd_option_license/LICENSE/"
	"/p:/cmdhomepage:/set the homepage field/cmd_option_homepage/HOMEPAGE/"
	"/a:/cmdauthor:/set the author field/cmd_option_author/AUTHOR/"
	"/b:/cmdblog:/set the blog field/cmd_option_blog/BLOG/"
	"/e:/cmdemail:/set the email field/cmd_option_email/EMAIL/"
	"/u:/cmdusage:/set the usage field/cmd_option_usage/USAGE/"
	"/o:/cmdoption:/add an option field/cmd_option_option/OPTION/"
	"/e:/cmdexample:/add an example field (first for usage)/cmd_option_example/EXAMPLE/"
	"/x:/cmdextrahelp:/set the extrahelp field/cmd_option_extrahelp/EXTRAHELP/"
	"/s:/cmdextranotes:/set the extranotes field/cmd_option_extranotes/EXTRANOTES/"
	"/y:/cmdday:/set the day field/cmd_option_day/DAY/"
	"/t:/cmdmonth:/set the month field/cmd_option_month/MONTH/"
	"/y:/cmdyear:/set the year field/cmd_option_year/YEAR/"
	"/s:/cmdtimestamp:/set the timestamp field/cmd_option_timestamp/TIMESTAMP/"
	"/i:/cmdinit:/set the init field/cmd_option_init/INIT/"
	"/m:/cmdmain:/set the main field/cmd_option_main/MAIN/"
)
cmd_extrahelp="All fields are mandatory except homepage, blog, email, extrahelp and extranotes, which can be empty."
cmd_extranotes="For more information, check documentation."
cmd_day="[@]pkgday[@]"
cmd_month="[@]pkgmonth[@]"
cmd_year="[@]pkgyear[@]"
cmd_timestamp="[@]pkgtimestamp[@]"
cmd_init="cmd_cmd_init"
cmd_main="cmd_cmd_main"

cmd_datadir="[@]pkgdatadir[@]"

# The cmd environment
source "$cmd_datadir/cmd.sh" "$@"
